package com.grandbroad.eim.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.easy.west.westclient.api.vod.bean.AlbumBean;
import com.easy.west.westclient.api.vod.bean.AlbumBeanList;
import com.easy.west.westclient.api.vod.event.GetAllAlbumEvent;
import com.easy.west.westclient.api.vod.event.GetHotAlbumEvent;
import com.flyco.tablayout.SlidingTabLayout;
import com.grandbroad.eim.R;
import com.grandbroad.eim.adapter.AlbumListAdapter;
import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.base.BaseFragment;
import com.grandbroad.eim.ui.activities.SearchActivity;
import com.grandbroad.eim.utils.UiUtlis;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 专辑列表
 */
public class AlbumFragment extends BaseFragment {
    @BindView(R.id.tab_album_layout)
    SlidingTabLayout tabAlbumLayout;
    @BindView(R.id.img_search)
    ImageView imgSearch;
    @BindView(R.id.top_album_bar)
    Toolbar topAlbumBar;
    @BindView(R.id.view_album_pager)
    ViewPager viewAlbumPager;
    private ViewPagerCusAdapter cusAdapter;
    private List<String> titles = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private AlbumListAdapter hotlistadpter;
    private AlbumListAdapter newlistadpter;
    @Override
    protected boolean setEventOpen() {
        return true;
    }

    @Override
    protected void initdata() {
        hotlistadpter = new AlbumListAdapter();
        newlistadpter = new AlbumListAdapter();
        //请求热门,请求最新
        MyApplication.getMoleclient().getHotAlbum();
        MyApplication.getMoleclient().getAllAlbum();
    }

    @Override
    protected void initview() {
        cusAdapter = new ViewPagerCusAdapter();
        viewAlbumPager.setAdapter(cusAdapter);
        tabAlbumLayout.setViewPager(viewAlbumPager,new String[]{"推荐","最新"});
        imgSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiUtlis.startAnimActivity((AppCompatActivity) getActivity(),SearchActivity.class,imgSearch,"search_img",true);
            }
        });
    }


    @Override
    protected int setlayout() {
        return R.layout.fragment_album;
    }

    /**
     * 列表适配器
      */
    class ViewPagerCusAdapter extends PagerAdapter
    {


        @Override
        public int getCount() {
            return 2;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = LayoutInflater.from(container.getContext()).inflate(R.layout.recycler_view,container,false);
            final RecyclerView re = view.findViewById(R.id.recycler_vod);
            if (position==0)
            {
                re.setLayoutManager(new LinearLayoutManager(container.getContext()));
                re.setAdapter(hotlistadpter);
            }
            else
            {
                re.setLayoutManager(new LinearLayoutManager(container.getContext()));
                re.setAdapter(newlistadpter);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view==o;
        }

    }

    /**
     * 获取热门的专辑列表
      * @param eva
     */
    @Subscribe(threadMode =  ThreadMode.MAIN)
    public void getHotAlbumlist(GetHotAlbumEvent eva)
    {
        if (eva.isOk())
        {
            AlbumBeanList beanList = eva.getBean();
            if (beanList.getStatus()==1)
            {
                hotlistadpter.setAlbumBeanList(beanList.getData());
                hotlistadpter.notifyDataSetChanged();
            }
        }
    }

    /**
     * 获取所有的
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getallAlbumList(GetAllAlbumEvent eva)
    {
        if (eva.isOk())
        {
            AlbumBeanList beanList = eva.getBean();
            if (beanList.getStatus()==1)
            {
                //先排序
                //按时间来
                orderAlbum(beanList.getData());


            }

        }
    }
    /**
     * 按时间顺序排列，大的在前面
     * @param data
     */
    private void orderAlbum(List<AlbumBean> data) {
        ListSort(data);
        newlistadpter.setAlbumBeanList(data);
        newlistadpter.notifyDataSetChanged();
    }
    /**
     * 根据时间排序
     * @param list
     */
    private static void ListSort(List<AlbumBean> list) {
        Collections.sort(list, new Comparator<AlbumBean>()
        {
            @Override
            public int compare(AlbumBean o1, AlbumBean o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date dt1 = format.parse(o1.getCreated_at());
                    Date dt2 = format.parse(o2.getCreated_at());
                    if (dt1.getTime() > dt2.getTime()) {
                        return -1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return 1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
}
